<template>
  <div class="container">
    <global-header :user="currentUser" />
    <loader
      v-if="isLoading"
      text="拼命加载中"
      background="rgba(0,0,0,0.8)"
    ></loader>
    <router-view></router-view>
    <footer class="text-center py-4 text-secondary bg-light mt-6">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">© 2020 知也专栏</li>
          <li class="list-inline-item">课程</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">联系</li>
          <li class="list-inline-item">更多</li>
        </ul>
      </small>
    </footer>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, watch } from 'vue'
import { useStore } from 'vuex'
import { GlobalDataProps } from './store'
import GlobalHeader from './components/GlobalHeader.vue'
import Loader from './components/Loader.vue'
import createMessage from './components/createMessage'
import 'bootstrap/dist/css/bootstrap.min.css'

export default defineComponent({
  name: 'App',
  components: {
    GlobalHeader,
    Loader
  },
  setup() {
    const store = useStore<GlobalDataProps>()
    const currentUser = computed(() => store.state.user)
    const isLoading = computed(() => store.state.loading)
    const error = computed(() => store.state.error)
    // 使用 watch 方法，当 error 发生变化的时候，watch 其中的某值，当某值是我们想要的结果的时候，我们就来显示这个错误提示
    watch(
      // 通过是否 error 来判断是否要改变
      () => error.value.status,
      () => {
        // 先拿到值
        const { status, message } = error.value
        // 如果status正常为true且有message值时
        if (status && message) {
          createMessage(message, 'error')
        }
      }
    )
    return {
      currentUser,
      isLoading,
      error
    }
  }
})
</script>
